package com.ph.order.api.controller;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cm.ph.shopping.facade.order.entity.PhManagerOrder;
import cm.ph.shopping.facade.order.entity.PhPurchaseList;
import cm.ph.shopping.facade.order.request.ManagerOrderRequest;
import cm.ph.shopping.facade.order.service.IManagerOrderService;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ph.base.BaseController;
import com.ph.shopping.common.core.customenum.ResultEnum;
import com.ph.shopping.common.util.com.order.Constants;
import com.ph.shopping.common.util.result.Result;
import com.ph.shopping.facade.permission.vo.SessionRoleVo;
import com.ph.shopping.facade.permission.vo.SessionUserVo;

@Controller
@RequestMapping("api/managerorder")
public class ManagerOrderController extends BaseController {

	@Reference(version = "1.0.0")
	private IManagerOrderService iManagerOrderService;

	/**
	 * 
	 * @Title: addPurchase
	 * @Description: 新增进货单
	 * @author WQiang
	 * @date 2017年3月20日 上午10:45:44
	 * @param request
	 * @return
	 */
	@RequestMapping("addpurchase")
	@ResponseBody
	public int addPurchase(List<PhPurchaseList> request) {
		try {
			// Agent agent = new Agent();
			SessionUserVo userVo = getLoginUser();
			for (PhPurchaseList purchase : request) {
				purchase.setUserId(userVo.getId());
			}
			return iManagerOrderService.insertPurchase(request);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.error("新增进货单失败!");
			e.printStackTrace();
			return Constants.FAIL;
		}
	}

	/**
	 * 
	 * @Title: addManagerOrder
	 * @Description: 新增进货订单
	 * @author WQiang
	 * @date 2017年3月20日 下午3:29:22
	 * @param managerOrder
	 * @return
	 */
	@RequestMapping("addmanageroder")
	@ResponseBody
	public int addManagerOrder(PhManagerOrder managerOrder) {
		try {
			return iManagerOrderService.insertManagerOrder(managerOrder);
		} catch (Exception e) {
			logger.error("新增进货订单失败");
			e.printStackTrace();
			return Constants.FAIL;
		}
	}

	/**
	 * 
	* @Title: listManagerOrders
	* @Description: 查询进货订单列表（代理商和商户的进货订单分开写，为了以后的财务结算方便）
	* @author WQiang
	* @date  2017年3月20日 下午8:39:37
	* @param orderRequest
	* @return
	 */
	@RequestMapping("managerorders")
	@ResponseBody
	public Result listManagerOrders(ManagerOrderRequest orderRequest) {
		
		SessionUserVo userVo = new SessionUserVo();
		SessionRoleVo roleVo = userVo.getSessionRoleVo().get(0);
		int roleCode = roleVo.getRoleCode();

		return getResult(ResultEnum.SUCCESS,
				iManagerOrderService.managerOrdersList(orderRequest.getPageNum(),orderRequest.getPageSize(), roleCode, userVo.getId()));
	}
}
